package org.jash.report.model

import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.launch
import org.jash.common.mvi.BaseState
import org.jash.common.mvi.BaseViewModel
import org.jash.common.net.retrofit
import org.jash.report.net.ReportService

class CommitViewModel:BaseViewModel<CommitIntent, CommitState>() {
    val service by lazy { retrofit.create(ReportService::class.java) }
    fun send(send: CommitIntent.Send) {
        viewModelScope.launch {
            state.value = try {
                val res = service.addReport(send.report)
                CommitState.ReportResponse(res.statusCode == 200)
            } catch (e:Exception) {
                BaseState.Error(e.message ?: "")
            }
        }
    }
}